package com.leadzone.dms.ctrl.index;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.leadzone.dms.ctrl.base.BaseController;
import com.leadzone.utils.CheckMobile;

@Controller
// @RequestMapping("/")
public class IndexController extends BaseController {
 
 

	@RequestMapping(value = "/ws")
	public ModelAndView ws() {
		return new ModelAndView("index");
	}

	@RequestMapping(value = "/ws1")
	public ModelAndView ws1() {
		return new ModelAndView("websocket/test1");
	}

	@RequestMapping(value = "/login")
	public ModelAndView login() {
		return new ModelAndView("pcindex");
	}

	@RequestMapping(value = "/")
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {

		boolean isFromMobile = false;
	 

		HttpSession session = request.getSession();
		// 检查是否已经记录访问方式（移动端或pc端）
		if (null == session.getAttribute("ua")) {
			try {

				// 获取ua，用来判断是否为移动端访问
				String userAgent = request.getHeader("USER-AGENT").toLowerCase();
				if (null == userAgent) {
					userAgent = "";
				}
				isFromMobile = CheckMobile.check(userAgent);
				// 判断是否为移动端访问
				if (isFromMobile) {

					System.out.println("移动端访问");
					session.setAttribute("ua", "mobile");

				} else {
					System.out.println("pc端访问");
					session.setAttribute("ua", "pc");

				}
			} catch (Exception e) {
			}
		} else {
			isFromMobile = session.getAttribute("ua").equals("mobile");
		}
//		if (isFromMobile)
//			 
//		else
//
//			return new ModelAndView("wxweb/oathqrcode");
// return "wxweb/oathqrcode";
         ModelAndView  v=  new ModelAndView("index");
		
	    v.addObject("page", "home"  );
		return v;
	}
//   所有页面入口
	@RequestMapping(value = "/{page}")
	public ModelAndView pcindex(@PathVariable String page) {
		ModelAndView  v=  new ModelAndView("index");
		
	    v.addObject("page",StringUtils.isBlank(page)?"home":page );
		return v;
	}
 

}
